package commerce.action;

import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import commerce.business.CommerceBusiness;
import commerce.hibernate.po.*;

public class ShopAction extends ActionSupport {
	private CommerceBusiness cb = new CommerceBusiness();
	private List shopList;
	private int shopCount;
	private Shop shop;
	private String shopId;

	public List getShopList() {
		return this.shopList;
	}

	public void setShopList(List shopList) {
		this.shopList = shopList;
	}

	public int getShopCount() {
		return this.shopCount;
	}

	public void setShopCount(int shopCount) {
		this.shopCount = shopCount;
	}

	public Shop getShop() {
		return this.shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public String getShopId() {
		return this.shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	
	public String list() {
		this.shopList = cb.getAllShop();
		this.shopCount = 0;
		if (this.shopList != null)
			this.shopCount = this.shopList.size();
		return "list";
	}
	public String edit(){
		view();
		return "edit";
	}
	public String saveedit()
	{
		if(getShop() == null ) {
			System.out.println("shop is null");
			addActionError(getText("SAVE_FAILED"));
			return "save";
		} 
		if(getShop().getShopName().equals(""))
		{
			addActionError(getText("SHOP_NAMEBLANK"));
			return "save";
		}
		if(cb.saveShop(shop)) {
			addActionMessage(getText("SAVE_SUCCESS"));			
		}
		else
		{
			addActionError(getText("SAVE_FAILED"));			
		}
		return "save";
	}
	public String delete() {
		if (this.getShopId() == null || this.getShopId().equals("")) {
			addActionError(getText("DELETE_FAILED"));
			list();//刷新列表结果
			return "afertdelete";
		} else {
			shop = cb.getShopById(Integer.parseInt(this.getShopId()));
			if (shop != null) {
				// System.out.println(shop.getShopName());
				if(cb.deleteShop(shop)==true)
				{
					addActionMessage(getText("DELETE_SUCCESS"));
				}
				else
				{
					addActionError(getText("DELETE_FAILED"));	
				}
			}
			else {
				System.out.println("shop is null");
				addActionError(getText("DELETE_FAILED"));				
			}
		}
		list();//刷新列表结果
		return "afertdelete";
	}
	
	public String view() {
		if (this.getShopId() == null || this.getShopId().equals("")) {			
			this.shop = null;
		} else {
			shop = cb.getShopById(Integer.parseInt(this.getShopId()));
			if (shop != null)
				System.out.println(shop.getShopName());
			else
				System.out.println("shop is null");
		}
		return "view";
		//return "success";
	}	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {

		return SUCCESS;
	}
}
